<template>
	<view>
		
	</view>
</template>

<script>
	import { getAddress, setAddress, setToken, getToken } from '@/config/storage.js'
	export default {
		data(){
			return{
				ref:''
			}
		},
		onLoad(option){
			if(option.ref){
				this.ref = option.ref
				this.$store.commit('user/setRef', option.ref)
			}
			this.login()
			// if(getToken()){
			// 	uni.reLaunch({
			// 		url:"/pages/tabbar/index"
			// 	})
			// }else{
			// 	this.login()
			// }
		},
		methods:{
			login(){
				this.$getWalletAddress().then(address=>{
					let localAddress = getAddress()
					// 账号没切换
					if(localAddress===address){
						this.$store.commit('user/setAddress', address)
						uni.switchTab({
							url:"/pages/tabbar/home"
						})
					}else{
						// 新地址走重新授权
						setAddress(address);
						let timestamp = Date.parse(new Date()) / 1000;
						let msg = 'Login-' + timestamp;
						this.$getSign(msg, address).then(sign => {
							this.$api.post('/data/api.login/login',{
								address,
								signature: sign,
								timestamp,
								inviter_code:this.ref
							}).then(res=>{
								setToken(res.token)
								this.$store.commit('user/setAddress', address)
								// 获取是否激活
								this.$api.post('/data/api.auth.center/get').then(res=>{
									this.$store.commit('user/setMineData', res.user)
									uni.switchTab({
										url:"/pages/tabbar/home"
									})
								})
							})
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">

</style>